package cn.iocoder.yudao.module.lfd.controller.admin.points;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.lfd.controller.admin.points.vo.*;
import cn.iocoder.yudao.module.lfd.dal.dataobject.points.PointsDO;
import cn.iocoder.yudao.module.lfd.convert.points.PointsConvert;
import cn.iocoder.yudao.module.lfd.service.points.PointsService;

@Tag(name = "管理后台 - 积分")
@RestController
@RequestMapping("/lfd/points")
@Validated
public class PointsController {

    @Resource
    private PointsService pointsService;

    @PostMapping("/create")
    @Operation(summary = "创建积分")
    @PreAuthorize("@ss.hasPermission('lfd:points:create')")
    public CommonResult<Long> createPoints(@Valid @RequestBody PointsCreateReqVO createReqVO) {
        return success(pointsService.createPoints(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新积分")
    @PreAuthorize("@ss.hasPermission('lfd:points:update')")
    public CommonResult<Boolean> updatePoints(@Valid @RequestBody PointsUpdateReqVO updateReqVO) {
        pointsService.updatePoints(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除积分")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lfd:points:delete')")
    public CommonResult<Boolean> deletePoints(@RequestParam("id") Long id) {
        pointsService.deletePoints(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得积分")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lfd:points:query')")
    public CommonResult<PointsRespVO> getPoints(@RequestParam("id") Long id) {
        PointsDO points = pointsService.getPoints(id);
        return success(PointsConvert.INSTANCE.convert(points));
    }

    @GetMapping("/list")
    @Operation(summary = "获得积分列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('lfd:points:query')")
    public CommonResult<List<PointsRespVO>> getPointsList(@RequestParam("ids") Collection<Long> ids) {
        List<PointsDO> list = pointsService.getPointsList(ids);
        return success(PointsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得积分分页")
    @PreAuthorize("@ss.hasPermission('lfd:points:query')")
    public CommonResult<PageResult<PointsRespVO>> getPointsPage(@Valid PointsPageReqVO pageVO) {
        PageResult<PointsDO> pageResult = pointsService.getPointsPage(pageVO);
        return success(PointsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出积分 Excel")
    @PreAuthorize("@ss.hasPermission('lfd:points:export')")
    @OperateLog(type = EXPORT)
    public void exportPointsExcel(@Valid PointsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PointsDO> list = pointsService.getPointsList(exportReqVO);
        // 导出 Excel
        List<PointsExcelVO> datas = PointsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "积分.xls", "数据", PointsExcelVO.class, datas);
    }

}
